package com.sunnay.util;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.sunnay.annotation.SunayField;
import com.sunnay.enums.FieldGroup;
import com.sunnay.exception.DataException;
import org.apache.commons.lang3.StringUtils;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;

public class FieldValidator {

    static final String FAIL_CODE = "-1";

    public FieldValidator() {
    }

    public static void validateFieldAndSuper(Object body) {
        validateFieldAndSuper(body, FieldGroup.DEFAULT);
    }

    public static void validateFieldAndSuper(Object body, FieldGroup fieldGroup) {
        try {
            if (body != null) {
                Class clazz = body.getClass();

                ArrayList fieldList;
                for(fieldList = new ArrayList(); clazz != null; clazz = clazz.getSuperclass()) {
                    fieldList.addAll(new ArrayList(Arrays.asList(clazz.getDeclaredFields())));
                }

                Field[] fields = new Field[fieldList.size()];
                fieldList.toArray(fields);
                Field.setAccessible(fields, true);
                foreachValidate(fields, body, fieldGroup);
            }

        } catch (IllegalAccessException var5) {
            IllegalAccessException e = var5;
            throw new DataException("系统异常:" + e.getMessage());
        }
    }

    private static void foreachValidate(Field[] fields, Object body, FieldGroup fieldGroup) throws IllegalAccessException {
        Field[] var3 = fields;
        int var4 = fields.length;

        for(int var5 = 0; var5 < var4; ++var5) {
            Field field = var3[var5];
            field.setAccessible(true);
            if (field.isAnnotationPresent(SunayField.class)) {
                SunayField classFieldAnnotation = (SunayField)field.getAnnotation(SunayField.class);
                boolean validateFieldGroup = validateFieldGroup(fieldGroup, classFieldAnnotation.fieldGroup());
                if (validateFieldGroup) {
                    if (!classFieldAnnotation.nullable() && null == field.get(body)) {
                        bssException(classFieldAnnotation, field.getName(), classFieldAnnotation.comment() + "不能为空");
                    }

                    if (!classFieldAnnotation.isEmpty() && (null == field.get(body) || StringUtils.isBlank((CharSequence)field.get(body)))) {
                        bssException(classFieldAnnotation, field.getName(), classFieldAnnotation.comment() + "不能为空");
                    }

                    int maxLength = classFieldAnnotation.maxLength();
                    int minLength = classFieldAnnotation.minLength();
                    int maxCharLength = classFieldAnnotation.maxCharLength();
                    int minCharLength = classFieldAnnotation.minCharLength();
                    boolean sp = classFieldAnnotation.specialChar();
                    Object fieldValue = field.get(body);
                    if (fieldValue instanceof String) {
                        String fieldValueStr = String.valueOf(fieldValue);
                        if (maxLength != -1 && fieldValueStr != null && fieldValueStr.length() > maxLength) {
                            bssException(classFieldAnnotation, field.getName(), classFieldAnnotation.comment() + "长度不能大于" + maxLength);
                        }

                        if (minLength != -1 && fieldValueStr != null && fieldValueStr.length() < minLength) {
                            bssException(classFieldAnnotation, field.getName(), classFieldAnnotation.comment() + "长度不能小于" + minLength);
                        }

                        int charLength = sp ? DataValidator.getCharlengthAndSpecial(fieldValueStr != null ? fieldValueStr : "") : DataValidator.getCharlength(fieldValueStr != null ? fieldValueStr : "");
                        if (maxCharLength != -1 && fieldValueStr != null && charLength > maxCharLength) {
                            bssException(classFieldAnnotation, field.getName(), classFieldAnnotation.comment() + "字符数最多" + maxCharLength);
                        }

                        if (minCharLength != -1 && fieldValueStr != null && charLength < minCharLength) {
                            bssException(classFieldAnnotation, field.getName(), classFieldAnnotation.comment() + "字符数最少" + minCharLength);
                        }

                        String dateFormat = classFieldAnnotation.strFormat();
                        if (StringUtils.isNotBlank(dateFormat)) {
                            boolean valid = LocalDateUtils.isValid(dateFormat, fieldValueStr);
                            if (!valid) {
                                bssException(classFieldAnnotation, field.getName(), classFieldAnnotation.comment() + "格式有误,正确:" + classFieldAnnotation.strFormat());
                            }
                        }
                    }

                    if (classFieldAnnotation.valid()) {
                        validateFieldAndSuper(field.get(body));
                    }

                    if (classFieldAnnotation.validateCollection() && Collection.class.isAssignableFrom(field.getType())) {
                        Collection collection = (Collection)field.get(body);
                        if (CollectionUtils.isEmpty(collection)) {
                            bssException(classFieldAnnotation, field.getName(), classFieldAnnotation.comment() + "不能为空");
                        }

                        Iterator iterator = collection.iterator();

                        while(iterator.hasNext()) {
                            validateFieldAndSuper(iterator.next());
                        }
                    }
                }
            }
        }

    }

    private static boolean validateFieldGroup(FieldGroup methodFieldGroup, FieldGroup annotationFieldGroup) {
        if (methodFieldGroup != FieldGroup.DEFAULT) {
            return annotationFieldGroup == FieldGroup.ADD_UPDATE || annotationFieldGroup == methodFieldGroup;
        } else {
            return true;
        }
    }

    private static void bssException(SunayField classFieldAnnotation, String field, String message) {
        if (StringUtils.isNotEmpty(classFieldAnnotation.errorText())) {
            throw new DataException(field, classFieldAnnotation.errorText(), "-1");
        } else {
            throw new DataException(field, message, "-1");
        }
    }
}
